<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel=StyleSheet HREF="../stylesheet/stylesheet.css" TYPE="text/css" MEDIA=screen>
<title>Shop Management System</title>
<?php include('menu.php'); ?>
<?php
	if(isset($_SESSION['user_group_id'])){
		if($_SESSION['user_group_id']!=1) {
		echo "<script>window.location = '../index.php';</script>";
   		exit();
		}
	}
?>
</head>
<body>
<?php
include("../connect/connect.php");

//แก้ไข
if($_GET["action"]=="update") {

	if($_POST['category_name'] != "" && $_POST['category_head'] != "")
		{
			$category_head = strtoupper($_POST['category_head']);
			$Query = "UPDATE category SET category_name = '".$_POST["category_name"]."',category_head = '$category_head' WHERE category_id = '".$_POST["category_id2"]."' ";
			$objQuery = mysql_query($Query);
			
			$sQuery = mysql_query("SELECT product_id,code_num FROM product WHERE category_id = '".$_POST['category_id2']."' ");
			while($sRow = mysql_fetch_array($sQuery)){
				$newCode = $category_head.$sRow['code_num'];
				mysql_query("UPDATE product SET code = '$newCode' WHERE product_id = '".$sRow['product_id']."'");
			}
			
			echo "<script type=\"text/javascript\"> window.location=\"product_manage_category.php\"</script>";
			exit();
		}
}

$Query = "SELECT * FROM category WHERE category_id = '".$_GET["id"]."'";
$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");
$row = mysql_fetch_array($objQuery);
?>
<form action="?action=update" name="formedit" method="post" id="formedit">
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading">
      <h1>แก้ไขหมวดสินค้า</h1>
    <div class="buttons"><a onclick="formedit.submit();" class="button"><span>บันทึก</span></a><a onclick="location = 'product_manage_category.php';" class="button"><span>ยกเลิก</span></a></div></div>
  <div class="content">
    <table class="form">
      <tr>
        <td>รหัสหมวดสินค้า : </td>
        <td><input name="category_id" type="text" disabled id="category_id" value="<?php echo $row["category_id"];?>" />
		<input name="category_id2" type="hidden" id="category_id2" value="<?php echo $row["category_id"];?>" /></td>
      </tr>
      <tr>
        <td>ชื่อหมวดสินค้า : </td>
        <td><input name="category_name" type="text" id="category_name" value="<?php echo $row["category_name"];?>" onkeyup="IsScharacter(this.value,this)" /></td>
      </tr>      
      <tr>
        <td>รหัสนำหน้าหมวดสินค้า : </td>
        <td><input name="category_head" type="text" id="category_head" onkeyup="noThaichar(this.value,this)" value="<?php echo $row["category_head"];?>" /><font color="red"> *หากเปลี่ยนรหัสนำหน้าหมวดรหัสบาร์โค้ดทั้งหมดที่อยู่ในหมวดนี้จะเปลี่ยนด้วยค่ะ</font></td>
      </tr>
    </table>
  </div>
</div>
</form>
</body>
<?php mysql_close(); ?>
</html>
